package com.sikaryofficial.backend.controller;

import cn.hutool.core.collection.CollUtil;
import com.sikaryofficial.backend.constant.RedisCacheKey;
import com.sikaryofficial.backend.domain.dto.req.PointReq;
import com.sikaryofficial.backend.domain.dto.resp.CountryGeoDTO;
import com.sikaryofficial.backend.manager.Ip2LocationManager;
import com.sikaryofficial.backend.service.IGadmCountryGeoService;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.utils.HiResultUtil;
import com.sikaryofficial.common.core.utils.ip.IpUtils;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.HiResult;
import com.sikaryofficial.common.redis.service.RedisService;
import com.sikaryofficial.system.api.model.ip.IpLocationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * GADM国家多边形数据Controller
 *
 * @author qinjinyuan
 * @date 2024-01-19
 */
@RestController
@RequestMapping("/gadm")
@Api(tags = "国家数据服务")
public class GadmCountryGeoController extends BaseController {
    @Autowired
    private IGadmCountryGeoService gadmCountryGeoService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private Ip2LocationManager ip2LocationManager;

    /**
     * GADM国家多边形数据
     */
    @PostMapping("/country")
    @ApiOperation("获取国家信息")
    public HiResult<CountryGeoDTO> getCountryInfo(@Validated @RequestBody PointReq req) {
        List<CountryGeoDTO> gadmCountryGeo = gadmCountryGeoService.findCountryByPoint(req.getLongitude(), req.getLatitude());
        if (CollUtil.isEmpty(gadmCountryGeo)) {
            return HiResultUtil.success();
        }
        return HiResultUtil.success(gadmCountryGeo.iterator().next());
    }

    /**
     * GADM国家多边形数据
     */
    @PostMapping("/countryList")
    @ApiOperation("国家列表")
    public HiResult<List<CountryGeoDTO>> getCountryInfo() {
        List<CountryGeoDTO> gadmCountryGeo = redisService.getCacheList(RedisCacheKey.COUNTRY_CODE_LIST_CACHE);
        if (CollUtil.isEmpty(gadmCountryGeo)) {
            gadmCountryGeo = Lists.newArrayList();
        }
        return HiResultUtil.success(gadmCountryGeo);
    }

    /**
     * 根据IP获取地理位置信息
     */
    @PostMapping("/getGeographyInfo")
    @ApiOperation("通过IP获取国家信息")
    public R<IpLocationVO> getGeographyInfo() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();
        String ip = IpUtils.getIpAddr(request);
        return R.ok(ip2LocationManager.getGeographyInfo(ip));
    }

    @GetMapping("/getCountryArea/{ip}")
    @ApiOperation("通过IP获取国家、区域、城市数据")
    public R<IpLocationVO> getCountryAreaVO(@PathVariable("ip") String ip) {
        return R.ok(ip2LocationManager.getCountryAreaVO(ip));
    }

}
